﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;        
using System.Xml.Schema;
using System.Xml.Xsl;


namespace WorkWithXML
{
    public partial class WorkWithXML : Form
    {
        private bool isValid = true;
        private XmlTextReader xml = null;
        private XmlSchemaCollection xsd = null;
        private XmlValidatingReader valid = null;
        private XslTransform xslt = null;
        
        public WorkWithXML()
        {
            InitializeComponent();

            textBoxXML.Enabled = false;
            textBoxXSD.Enabled = false;
            textBoxXSLT.Enabled = false;
            
            textBoxXML.Text = "";
            textBoxXSD.Text = "";
            textBoxXSLT.Text = "";

            buttonXMLPath.Enabled = false;
            buttonXSDPath.Enabled = false;
            buttonXSLTPath.Enabled = false;
            buttonGo.Enabled = false;
            
        }

        private void radioButtonValidateSchema_CheckedChanged(object sender, EventArgs e)
        {
            textBoxXML.Text = "";
            textBoxXSD.Text = "";
            textBoxXSLT.Text = "";

            if (radioButtonValidateSchema.Checked)
            {
                textBoxXML.Enabled = true;
                textBoxXSD.Enabled = true;
                textBoxXSLT.Enabled = false;
                buttonXMLPath.Enabled = true;
                buttonXSDPath.Enabled = true;
                buttonXSLTPath.Enabled = false;
                buttonGo.Enabled = true;
            }
            else
            {
                textBoxXML.Enabled = false;
                textBoxXSD.Enabled = false;
                textBoxXSLT.Enabled = false;
                buttonXMLPath.Enabled = false;
                buttonXSDPath.Enabled = false;
                buttonXSLTPath.Enabled = false;
                buttonGo.Enabled = false;
            }
        }

        private void radioButtonTrnaformXML_CheckedChanged(object sender, EventArgs e)
        {
            textBoxXML.Text = "";
            textBoxXSD.Text = "";
            textBoxXSLT.Text = "";

            if (radioButtonTrnaformXML.Checked)
            {
                textBoxXML.Enabled = true;
                textBoxXSD.Enabled = false;
                textBoxXSLT.Enabled = true;
                buttonXMLPath.Enabled = true;
                buttonXSDPath.Enabled = false;
                buttonXSLTPath.Enabled = true;
                buttonGo.Enabled = true;
            }
            else
            {
                textBoxXML.Enabled = false;
                textBoxXSD.Enabled = false;
                textBoxXSLT.Enabled = false;
                buttonXMLPath.Enabled = false;
                buttonXSDPath.Enabled = false;
                buttonXSLTPath.Enabled = false;
                buttonGo.Enabled = false;
            }
        }

        private void buttonGo_Click(object sender, EventArgs e)
        {
            isValid = true;
            if ((textBoxXML.Text != "") && (textBoxXSD.Text != ""))
            {
                xml = new XmlTextReader(textBoxXML.Text);
                valid = new XmlValidatingReader(xml);
                xsd = new XmlSchemaCollection();

                xsd.Add(null, textBoxXSD.Text);
                valid.Schemas.Add(xsd);
                valid.ValidationType = ValidationType.Schema;
                valid.ValidationEventHandler += new ValidationEventHandler(ErrorValidation);
                while (valid.Read()) ;
                valid.Close();
                
                if (isValid)
                    MessageBox.Show("XML is valid");
                else
                    MessageBox.Show("XML is not valid");                
            }
            else
                if ((textBoxXML.Text != "") && (textBoxXSLT.Text != ""))
                {
                    xslt = new XslTransform();
                    xslt.Load(textBoxXSLT.Text);
                    
                    try
                    {
                        xslt.Transform(textBoxXML.Text, textBoxXML.Text + ".out.xml");
                    }
                    catch (Exception except)
                    {
                        MessageBox.Show(except.Message);
                    }

                    MessageBox.Show("Transform file has been created.");
                }
        }

        // event handler
        private void ErrorValidation(object sender, ValidationEventArgs args)
        {
            isValid = false;
            MessageBox.Show("Validation event\n" + args.Message);
        }

        private void Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void buttonXSLTPath_Click(object sender, EventArgs e)
        {
            openFileDialog.Title = "Open XSL File";
            openFileDialog.Filter = "XSL Files|*.xsl";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                textBoxXSLT.Text = openFileDialog.FileName;
            }
        }

        private void buttonXSDPath_Click(object sender, EventArgs e)
        {
            openFileDialog.Title = "Open XSD File";
            openFileDialog.Filter = "XSD Files|*.xsd";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                textBoxXSD.Text = openFileDialog.FileName;
            }
        }

        private void buttonXMLPath_Click(object sender, EventArgs e)
        {
            openFileDialog.Title = "Open XML File";
            openFileDialog.Filter = "XML Files|*.xml";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                textBoxXML.Text = openFileDialog.FileName;
            }
        }
    }
}
